package com.xiliubit.cloudpicture.manager.upload;

import cn.hutool.core.io.FileUtil;
import com.xiliubit.cloudpicture.exception.BusinessException;
import com.xiliubit.cloudpicture.exception.ErrorCode;
import com.xiliubit.cloudpicture.exception.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class FIlePictureUpload extends PictureUploadTemplate {

//    @Resource
//    private CosManager cosManager;

    /*
      上传图片

      @param multipartFile 图片
     * @param uploadPathPrefix 上传路径前缀
     */
/*    public PictureUploadResult putPicture(MultipartFile multipartFile, String uploadPathPrefix) {
        // 校验图片
        validPicture(multipartFile);
        // 图片地址
        String originalFilename = getOriginalFilename(multipartFile);
        String Suffix = FileUtil.getSuffix(originalFilename);
        String uploadFilename = String.format("%s_%s.%s",
                LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
                RandomUtil.randomString(8), Suffix);
        String uploadPath = String.format("/%s/%s", uploadPathPrefix, uploadFilename);

        // 文件目录
        String filepath = String.format("/test/%s", originalFilename);
        File file = null;
        try {
           // 创建临时文件
            file = File.createTempFile(filepath, null);
            processFile(multipartFile, file);
            // 上传文件
            PutObjectResult putObjectResult = cosManager.putPictureObject(file, uploadPath);
            // 封装返回结果
            return buildResult(putObjectResult, originalFilename, uploadPath,file);
        } catch (Exception e) {
            log.error("文件上传失败: {}", filepath, e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
        } finally {
            deleteTempFile(file, filepath);
        }
    }*/

    /**
     * 校验图片
     *
     * @param inputSource 源文件
     */
    @Override
    public String validPicture(Object inputSource) {
        MultipartFile multipartFile = (MultipartFile) inputSource;
        // 不能为空
        ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR);
        // 大小不能超过3mb
        final long ONE_M = 1024 * 1024L;
        ThrowUtils.throwIf(multipartFile.getSize() > ONE_M * 3, ErrorCode.PARAMS_ERROR, "图片大小不能超过3M");
        // 校验后缀
        // 获取文件后缀
        String suffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        // 校验后缀
        final List<String> fileSuffixList = Arrays.asList("png", "jpg", "jpeg", "webp");
        ThrowUtils.throwIf(!fileSuffixList.contains(suffix), ErrorCode.PARAMS_ERROR, "文件类型错误");

        // 返回后缀
        return suffix;
    }

    /**
     * 获取原始文件名
     *
     * @param inputSource 源文件
     * @return 原始文件名
     */
    @Override
    public String getOriginalFilename(Object inputSource) {
        MultipartFile multipartFile = (MultipartFile) inputSource;
        return multipartFile.getOriginalFilename();
    }

    /**
     * 下载文件到本地作为中转
     *
     * @param inputSource 源文件
     * @param file 中转文件
     */
    @Override
    public void processFile(Object inputSource, File file) {
        MultipartFile multipartFile = (MultipartFile) inputSource;
        try {
            multipartFile.transferTo(file);
        } catch (Exception e) {
            log.error("文件下载失败: {}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
    }

}
